package cn.justforchris.system.controller;




import cn.justforchris.system.domain.Role;
import cn.justforchris.system.query.RoleQuery;
import cn.justforchris.system.service.IRoleService;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户接口
 */
@RestController
@RequestMapping("/role")
@Api(tags = "用户接口")
public class RoleController {

    @Autowired
    private IRoleService RoleService;

    /**
     * 新增
     * @param
     * @return
     */
    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Role Role) {
       try {
           if (Role.getId() == null) {
            RoleService.insertRole(Role);
        } else {
            RoleService.updateRole(Role);
        }
        return JsonResult.success();
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.error();
       }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deletRole(@PathVariable Long id){
        try{
            RoleService.deleteRole(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelet(@RequestBody Long[] ids){
        try{
            RoleService.patchDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Role getRole(@PathVariable Long id){

        return RoleService.getRoleById(id);

    }

    /**
     * 获取所有
     * @return
     */
    @GetMapping
    public List<Role> getAll(){
        return RoleService.getAll();
    }

    /**
     * 分页查询
     * @param queryPage
     * @return
     */
    @PostMapping
    public PageInfo<Role> queryPage(@RequestBody RoleQuery queryPage){
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return RoleService.queryPage(queryPage);

    }









}
